package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.AutoFillConstant;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Dish;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.mapper.AdminOrderMapper;
import com.sky.result.PageResult;
import com.sky.service.AdminOrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.Period;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Sabrina
 * @Date:2025/06/15/20:56
 * @Description:
 */
@Service
public class AdminOrderServiceImpl implements AdminOrderService {
    @Autowired
    private AdminOrderMapper adminOrderMapper;

    /**
     * 订单搜索
     * @param dto
     * @return
     */
    @Transactional
    @Override
    public PageResult orderSearch(OrdersPageQueryDTO dto) {
        //1.分页查询
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<Orders> page = adminOrderMapper.orderSearch(dto);

        //2.给前端返回的是orders里的数据+orderDishes,用vo封装的
        List<Orders> orders = page.getResult();
        ArrayList<OrderVO> orderVOS = new ArrayList<>();
        for (Orders order : orders) {
            List<OrderDetail> details = adminOrderMapper.getOrderDetails(order.getId());
            //拿到orderDishes
            String orderDishes = new String();
            for (OrderDetail detail : details) {
                orderDishes = orderDishes + ","+detail.getName();
            }
            OrderVO orderVO = new OrderVO();
            BeanUtils.copyProperties(order,orderVO);
            orderVO.setOrderDishes(orderDishes);
            orderVOS.add(orderVO);
        }
        return new PageResult(page.getTotal(),orderVOS);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @Override
    public OrderVO getOrderById(Long id) {
        //1.先查到orders
        Orders orders = adminOrderMapper.getOrderById(id);

        //2.再查order_detail
        List<OrderDetail> orderDetails = adminOrderMapper.getOrderDetails(id);

        //封装OrderVO返回给controller
        OrderVO orderVO = new OrderVO();
        BeanUtils.copyProperties(orders,orderVO);
        orderVO.setOrderDetailList(orderDetails);

        return orderVO;
    }

    /**
     * 取消订单
     * @param dto
     */
    @Override
    public void cancel(OrdersCancelDTO dto) {
        Orders orders = new Orders();
        BeanUtils.copyProperties(dto,orders);
        orders.setCancelTime(LocalDateTime.now());//取消时间
        orders.setStatus(Orders.CANCELLED);
        //退款
        //这里需要调用退款的方法或者接口
        orders.setPayStatus(Orders.REFUND);
        adminOrderMapper.update(orders);
    }

    /**
     * 拒单
     * @param dto
     */
    @Override
    public void rejection(OrdersRejectionDTO dto) {
        Orders orders = new Orders();
        BeanUtils.copyProperties(dto,orders);
        orders.setStatus(Orders.CANCELLED);
        //退款
        //这里需要调用退款的方法或者接口
        orders.setPayStatus(Orders.REFUND);
        adminOrderMapper.update(orders);
    }

    /**
     * 接单
     * @param dto
     */
    @Override
    public void confirm(OrdersConfirmDTO dto) {
        Orders orders = new Orders();
        BeanUtils.copyProperties(dto,orders);
        orders.setStatus(Orders.CONFIRMED);
        adminOrderMapper.update(orders);
    }

    /**
     * 派送
     * @param id
     */
    @Override
    public void deliver(Long id) {
        Orders orders = new Orders();
        orders.setId(id);
        orders.setStatus(Orders.DELIVERY_IN_PROGRESS);
//        orders.setEstimatedDeliveryTime(LocalDateTime.now());
        adminOrderMapper.update(orders);
    }

    /**
     * 完成订单
     * @param id
     */
    @Override
    public void complete(Long id) {
        Orders orders = new Orders();
        orders.setId(id);
        orders.setStatus(Orders.COMPLETED);
        orders.setDeliveryTime(LocalDateTime.now());
        adminOrderMapper.update(orders);
    }

    /**
     * 各个状态的订单数量统计
     * @return
     */
    @Override
    public OrderStatisticsVO countOrders() {
        return adminOrderMapper.getStatusCounts();
    }
}
